import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * JDBC执行DML语句
 * 修改userinfo中Tony的密码为:654321
 * UPDATE userinfo SET password='654321' WHERE username='Tony'
 */
public class JDBCDemo2 {
    public static void main(String[] args) {
        /*
            1.加载驱动 Class.forName()
            2.连接对象 DriverManger.getConnection(...)
            3.执行对象 connection.createStatement()
            4.执行语句 statement.executeUpdate("SQL")
            5.断开连接 connection.close()
         */
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true","root","root");
            Statement statement = connection.createStatement();
            // 接收用户输入
            Scanner scanner = new Scanner(System.in);
            System.out.println("修改密码功能");
            System.out.println("请输入用户名:");
            String username = scanner.nextLine();
            System.out.println("请输入新密码:");
            String password = scanner.nextLine();

            String sql = "UPDATE userinfo SET password='"+password+"' WHERE username='"+username+"'";
            int i = statement.executeUpdate(sql); // i:返回受影响的数据条数
            connection.close();
            if (i > 0){
                System.out.println("密码修改成功");
            }else{
                System.out.println("密码修改失败");
            }

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }
}










